﻿
Partial Class WebForm_ManageOrganization
    Inherits System.Web.UI.Page

    Protected Sub trOrganization_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles trOrganization.SelectedNodeChanged
        'Response.Write(Me.trOrganization.SelectedNode.Text)

        'Me.ClientScript.RegisterClientScriptBlock(Me.GetType, "contextmenu", "showmenuie5(e);", True)
    End Sub

    Protected Sub ContextMenu1_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles ContextMenu1.MenuItemClick

        If e.Item.Value = "Add" Then
            Me.AddOrganization1.Show(Me.OrganizationID)
            Me.TabContainer1.ActiveTab = Me.AddTabPanel
        ElseIf e.Item.Value = "AddInternal" Then
            Me.AddOrganization1.Show(Me.OrganizationID)
            Me.TabContainer1.ActiveTab = Me.AddTabPanel
        ElseIf e.Item.Value = "Edit" Then
            Me.AddOrganization1.ShowEdit(Me.OrganizationID)
            Me.TabContainer1.ActiveTab = Me.AddTabPanel
        ElseIf e.Item.Value = "Disable" Then
            Me.ChangeActiveStatus("N")
            Me.BindOrganization()
        ElseIf e.Item.Value = "Enable" Then
            Me.ChangeActiveStatus("Y")
            Me.BindOrganization()
        ElseIf e.Item.Value = "Cut" Then
            Me.Cutting = True
            Me.CuttedID = Me.OrganizationID
        ElseIf e.Item.Value = "Paste" Then
            If Me.Cutting = False Then
                MyObj.ShowMessage("You need choose item!!")
            Else
                Me.Cutting = False
                Me.ChangeParent(Me.trOrganization.SelectedNode.Value)
                Me.BindOrganization()
            End If
        End If
    End Sub

    Protected Sub AddOrganization1_OrganizationAdded(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddOrganization1.OrganizationAdded
        Me.AddData()
    End Sub

    Protected Sub AddOrganization1_OrganizationUpdated(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddOrganization1.OrganizationUpdated
        Me.UpdateData()
    End Sub

    Protected Sub AddOrganization1_Saved(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddOrganization1.Saved
        Me.TabContainer1.ActiveTab = Me.SearchTabPanel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindDDLActive()
            Me.BindOrganization()
        End If
    End Sub

    Protected Sub ddlActive_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlActive.SelectedIndexChanged
        Me.BindOrganization()
    End Sub

End Class

Partial Class WebForm_ManageOrganization
    Private Function AddData() As Boolean
        Me.BindOrganization()
        Return True
        Dim node As New My.MyTreeNode

        node.MyText = Me.AddOrganization1.OrganizationName
        node.Value = Me.AddOrganization1.OrganizationID

        Me.trOrganization.SelectedNode.ChildNodes.Add(node)

    End Function

    Private Function UpdateData() As Boolean
        Me.BindOrganization()

    End Function

    Private Sub BindDDLActive()
        Me.ddlActive.DataTextField = "Text"
        Me.ddlActive.DataValueField = "Value"
        Me.ddlActive.DataSource = MyObj.GetCodeMappingData("Organization", "Active")
        Me.ddlActive.DataBind()

        Me.ddlActive.Items.Insert(0, MyObj.GetDDLSelectAllItem())
        Me.ddlActive.SelectedIndex = Me.ddlActive.Items.IndexOf(Me.ddlActive.Items.FindByValue("Y"))
    End Sub

    Private Sub BindOrganization()

        Me.trOrganization.Nodes.Clear()

        Dim node As New My.MyTreeNode

        node.MyText = "L'OREAL Taiwan"
        node.Value = "null"

        Me.trOrganization.Nodes.Add(node)

        Me.BindOrganization(node)

        Me.trOrganization.ExpandAll()
    End Sub

    Private Sub BindOrganization(ByVal node As TreeNode)
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select o.* ")
        sql.AppendFormat("from Organization o ")

        ' Root
        If node.Value = "null" Then
            sql.AppendFormat("where o.Parent is null ")
        Else
            sql.AppendFormat("where o.Parent = {0} ", node.Value)
        End If

        If Me.ddlActive.SelectedValue <> "" Then
            sql.AppendFormat("and o.Active = '{0}' ", Me.ddlActive.SelectedValue)
        End If

        For Each dr As Data.DataRow In dat.GetData(sql.ToString(), Nothing, Data.CommandType.Text, "dt").Tables(0).Rows
            Dim child As New My.MyTreeNode()

            If dr("Internal") = "Y" Then
                child.BackColor = Drawing.Color.LightGreen
            End If

            child.MyText = dr("OrganizationName")
            child.Value = dr("OrganizationID")

            If dr("Active") = "N" Then
                child.MyText = "<b><font color=red>X</font></b> " + dr("OrganizationName")
            End If

            node.ChildNodes.Add(child)
            Me.BindOrganization(child)
        Next

    End Sub

    Private Function ChangeActiveStatus(ByVal status As String) As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("update Organization set ")
        sql.AppendFormat(" Active = '{0}' ", status)
        sql.AppendFormat("where OrganizationID = '{0}' ", Me.OrganizationID)

        Using scope As New Transactions.TransactionScope
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)
            scope.Complete()
        End Using
    End Function

    Private Function ChangeParent(ByVal parentID As String) As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("update Organization set ")
        sql.AppendFormat(" Parent = {0} ", parentID)
        sql.AppendFormat("where OrganizationID = '{0}' ", Me.CuttedID)

        Using scope As New Transactions.TransactionScope
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            clsAuth.BuildOrganizationCode()
            clsAuth.BuildRoleOrganizationMappingData()

            scope.Complete()
        End Using
    End Function

    Private ReadOnly Property OrganizationID() As String
        Get
            Return Me.trOrganization.SelectedNode.Value
        End Get
    End Property

    Private Property Cutting() As Boolean
        Get
            Return ViewState("Cutting")
        End Get
        Set(ByVal value As Boolean)
            ViewState("Cutting") = value
        End Set
    End Property

    Private Property CuttedID() As String
        Get
            Return ViewState("CuttedID")
        End Get
        Set(ByVal value As String)
            ViewState("CuttedID") = value
        End Set
    End Property
End Class
